/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.domain;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static at.irian.demo.jsfatwork.domain.DomainQueryDetails.*;

/**
 * @author JSF@Work Project Team
 */
@Entity
@Table(name = "T_Vote")
@NamedQuery(name = FIND_ALL_VOTES_OF_USER,
        query = "select v from Vote v where v.votedBy = " + C_PARAMETER_USER)
public class Vote extends AbstractDomainObject
{
    private static final long serialVersionUID = 8654127366095431326L;

    public Vote()
    {
    }

    public Vote(Poll poll, User votedBy)
    {
        this.poll = poll;
        this.votedBy = votedBy;
    }

    @Column
    private boolean active;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    private Date votingDate;

    public void addAnswer(Answer answer)
    {
        this.answers.add(answer);
    }

    @PrePersist
    @PreUpdate
    protected void defineVotingDate()
    {
        this.votingDate = new Date();
    }

    @ManyToOne(optional = false, cascade = {})
    private Poll poll;

    @Basic(optional = false)
    @OneToMany(cascade = {})
    private List<Answer> answers = new ArrayList<Answer>();

    @ManyToOne(optional = false)
    private User votedBy;

    /*
     * generated
     */

    public List<Answer> getAnswers()
    {
        return answers;
    }

    public User getVotedBy()
    {
        return votedBy;
    }

    public boolean isActive()
    {
        return active;
    }

    public void setActive(boolean active)
    {
        this.active = active;
    }

    public Date getVotingDate()
    {
        return votingDate;
    }

    public Poll getPoll()
    {
        return poll;
    }
}
